//
//  YLMemberController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/11.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMemberController.h"
#import "YLMemberHeader.h"
#import "YLGlobeHeader.h"
#import "YLMyPrivilegeHeader.h"
#import "YLMemberViewModel.h"
#import "YLImageTitleCollectionViewCell.h"
#import "YLMemberPrivilegeViewController.h"
#import "YLPrivilegeDetailViewController.h"
#import "YLMemberLevelViewModel.h"
#import "YLMemberItem.h"
#import "YLMemberLevelModel.h"
@interface YLMemberController ()<UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) YLMemberViewModel *viewModel;

@property (nonatomic, strong) YLMemberLevelViewModel *levelViewModel;

@property (nonatomic, strong) YLMemberHeader *memberHeader;
@end

@implementation YLMemberController

- (YLMemberLevelViewModel *)levelViewModel
{
    if (!_levelViewModel) {
        _levelViewModel = [[YLMemberLevelViewModel alloc] init];
    }
    return _levelViewModel;
}

- (YLMemberViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLMemberViewModel alloc] init];
    }
    return _viewModel;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"会员";
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[YLMemberHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[YLMemberHeader headerID]];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YLMyPrivilegeHeader class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[YLMyPrivilegeHeader headerID]];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YLImageTitleCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:[YLImageTitleCollectionViewCell cellID]];
    
    WHNavBackButton *backButton = [WHNavBackButton navBackButton];
    [backButton setImage:[UIImage imageNamed:@"fanhuijianblack"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.collectionView.alwaysBounceVertical = YES;
    
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.privilegeListCommend execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf.collectionView reloadData];
    }];
    
    [[self.levelViewModel.requestCommend execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf.collectionView reloadData];
    }];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:YLColor(50, 50, 50)};
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBackground"] forBarMetrics:UIBarMetricsDefault];
    // 修改标题颜色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(YLScreenW, YLScreenW * 377 / 750.0);
    } else if (section == 1) {
        return CGSizeMake(YLScreenW, 42);
    }
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            YLMemberHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[YLMemberHeader headerID] forIndexPath:indexPath];
            if (self.levelViewModel.levelModel) {
                header.model = self.levelViewModel.levelModel;
            }
            
            return header;
        } else if (indexPath.section == 1) {
            __weak typeof(self) weakSelf = self;
            YLMyPrivilegeHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[YLMyPrivilegeHeader headerID] forIndexPath:indexPath];
            if (self.levelViewModel.levelModel) {
                header.showTitle = self.levelViewModel.levelModel.showTitle;
            }
            header.clickBlock = ^{
                YLPrivilegeDetailViewController *detail = [[YLPrivilegeDetailViewController alloc] init];
                [weakSelf.navigationController pushViewController:detail animated:YES];
            };
            return header;
        }
    }
    return [[UICollectionReusableView alloc] init];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    return [self initWithCollectionViewLayout:layout];
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 1) {
        return self.viewModel.items.count;
    }
    return 0;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YLImageTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[YLImageTitleCollectionViewCell cellID] forIndexPath:indexPath];
    cell.item = self.viewModel.items[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(YLScreenW / 5, YLScreenW / 5.0  + 30);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    CGFloat w = (YLScreenW - YLScreenW / 5 * 4.0) / 5.0;
    return UIEdgeInsetsMake(0, w, 0, w);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return (YLScreenW - YLScreenW / 5 * 4.0) / 5.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{
    YLMemberPrivilegeViewController *privilege = [[YLMemberPrivilegeViewController alloc] init];
    YLMemberItem *item = self.viewModel.items[indexPath.row];
    privilege.privilegeId = item.privilegeID;
    [self.navigationController pushViewController:privilege animated:YES];
}


@end
